package pos.presentacion;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pos.dominio.Usuario;
import pos.dominio.UsuarioManager;


public class FrontController extends HttpServlet {
	String user = "";
	String passwd = "";
	
	private List<Usuario> lista = new LinkedList<Usuario>();
	
	public void init() throws ServletException {
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {

		if(logado(request)){
				String resource = request.getParameter("res");
				RequestDispatcher d = request.getRequestDispatcher(resource);
				if(d!=null){
					d.forward(request,response);
				}
		}else{
			if(datosVacio(request)==true){
				response.sendRedirect("errorRegistro.jsp");
			}else if(datosVacio(request)==false){
				response.sendRedirect("error.jsp");
			}
		}
		
	} 
		
	private boolean datosVacio(HttpServletRequest request){
		boolean res=false;
		String userForm=request.getParameter("user");
		String passwdForm=request.getParameter("passwd");
		HttpSession session=request.getSession(false);
		session = request.getSession();
		if (userForm == null || passwdForm == null || userForm.length()==0 || passwdForm.length()==0){
			res = true;
		} else{
			res = false;
			session.setAttribute("session.user", userForm);
			session.setAttribute("session.password", passwdForm);
		}
		return res;
	}
	
	private boolean logado(HttpServletRequest request) {
		boolean logado = false;

		HttpSession session = request.getSession(false);

		String userForm = request.getParameter("user");
		String passwdForm = request.getParameter("passwd");

		if (session == null) {
			session = request.getSession();
			if (userForm == null || passwdForm == null
					|| userForm.length() == 0 || passwdForm.length() == 0) {
				logado = false;

			} else {
				if (valido(userForm, passwdForm)) {
					logado = true;
					session.setAttribute("session.user", userForm);
				} else {
					logado = false;
				}

			}

		} else {
			if (userForm == null || passwdForm == null) {
				logado = true;
			} else {
				if (valido(userForm, passwdForm)) {
					logado = true;
					session.setAttribute("session.user", userForm);
				} else {
					logado = false;
				}

			}
		}
		return logado;
	}

	public boolean valido(String userForm, String passForm) {
		boolean res = false;
		lista = UsuarioManager.getInstance().getUsuarios();
		for(Usuario u :lista){
		 user= u.getUsuarioID();
		 passwd=u.getPassword();
		 	
			if((userForm.equals(this.user) && passForm.equals(this.passwd))){
				res=true;
				break;
			}
		}
		return res;

	}

}